package com.github.alenfive.examples;

import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
SELECT * FROM t_user WHERE 1=1
<#if username?? && username?trim?length gt 0>
    AND F_username = :username
</#if>

SELECT * FROM t_user WHERE 1=1
<#if ids?? && ids?size gt 0>
    AND F_id IN (:ids)
</#if>
*/

/**
 * #### A) 变量是否为空或者不存在的判断
 * * A-01) key是否存在
 * * A-02) key对应的value是否为null
 * <#if key??>
 *
 * <#else>
 *
 * </#if>
 *
 * #### B) 子属性使用或者方法的使用
 * *  B-01) 要先判断变量是否为null或者是否存在后
 * *  B-02) 才可以使用变量?trim?length等函数或者属性
 * <#if field?? && field?trim?length gt 0>
 *
 * <#else>
 *
 * </#if>
 *
 *  #### C) 集合的非空判断
 *  <#if ids?? && ids?size gt 0>
 *
 *  <#else>
 *
 *  </#if>
 */
public class FreemarkerExamples {

    public static void main(String[] args) throws Exception {
        example01();

//        strTest();
//
//        collectionTest();

    }

    public static void example01() throws Exception {
        // 1.创建一个 Configuration 对象， 参数是freemarker 的版本号
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);

        // 2.通过字符串创建模板
        StringTemplateLoader templateLoader = new StringTemplateLoader();
        configuration.setTemplateLoader(templateLoader);
        configuration.setDefaultEncoding("UTF-8");

        String templateName = "templateName";
        String sql = "SELECT * FROM t_user WHERE 1=1 " +
                "<#if username?? && username?trim?length gt 0>" +
                " AND F_username = :username " +
                "</#if>";
        Template template = new Template(templateName, sql, configuration);
        Map<String, Object> params = new HashMap(4);
        params.put("username", null);
        StringWriter stringWriter = new StringWriter();
        template.process(params, stringWriter);
        System.out.println(stringWriter.toString());

        stringWriter = new StringWriter();
        params.put("username", "username");
        template.process(params, stringWriter);
        System.out.println(stringWriter.toString());
    }

    /**
     * #### A) 变量是否为空或者不存在的判断
     * * A-01) key是否存在
     * * A-02) key对应的value是否为null
     * <#if key??>
     *
     * <#else>
     *
     * </#if>
     *
     * #### B) 子属性使用或者方法的使用
     * *  B-01) 要先判断变量是否为null或者是否存在后
     * *  B-02) 才可以使用变量?trim?length等函数或者属性
     * <#if field?? && field?trim?length gt 0>
     *
     * <#else>
     *
     * </#if>
     */
    public static void strTest() throws Exception {
        Configuration configuration = getConfiguration();
        String sql = "SELECT * FROM t_user WHERE 1=1 \n" +
            "<#if username?? && username?trim?length gt 0> \n" +
            "  AND F_username = :username \n" +
            "</#if> \n";
        System.out.println(sql);
        Template template = new Template("templateName", sql, configuration);
        // params参数都没有时
        Map<String, Object> params = new HashMap(4);
        printTemplateResultStr(params, template);

        params.put("username", null);
        printTemplateResultStr(params, template);

        params.put("username", "");
        printTemplateResultStr(params, template);

        params.put("username", " ");
        printTemplateResultStr(params, template);

        params.put("username", " root ");
        printTemplateResultStr(params, template);

    }

    public static void collectionTest() throws Exception {
        Configuration configuration = getConfiguration();
        String sql = "SELECT * FROM t_user WHERE 1=1 \n" +
                "<#if ids?? && ids?size gt 0> \n" +
                "  AND F_id IN (:ids) \n" +
                "<#else> \n" +
                " AND 1=2 \n" +
                "</#if> \n";
        System.out.println(sql);
        Template template = new Template("templateName", sql, configuration);
        // params参数都没有时
        Map<String, Object> params = new HashMap(4);
        printTemplateResultStr(params, template);

        params.put("ids", null);
        printTemplateResultStr(params, template);

        params.put("ids", new ArrayList());
        printTemplateResultStr(params, template);

        params.put("ids", Arrays.asList("1", "2", "3"));
        printTemplateResultStr(params, template);

    }

    public static void objTest() {

    }

    public static void printTemplateResultStr(Map<String, Object> params, Template template) throws Exception {
        StringWriter stringWriter = new StringWriter();
        // params参数都没有时
        template.process(params, stringWriter);
        System.out.println(stringWriter.toString());
    }

    public static Configuration getConfiguration() {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        StringTemplateLoader templateLoader = new StringTemplateLoader();
        configuration.setTemplateLoader(templateLoader);
        configuration.setDefaultEncoding("UTF-8");
        return configuration;
    }

}
